<?php
require_once('db.inc.php');
require_once('functions.php');

if (!defined('EXCHANGE_TABLE_NAME'))
	define ('EXCHANGE_TABLE_NAME', 'exchange');
if (!defined('EXCHANGE_UNITY'))
	define ('EXCHANGE_UNITY', '1');

class Exchange {
	var $id;
	var $currency_from;
	var $currency_to;
	var $rate;

	function Exchange($id=0)
	{
		$this->id = $id;
		if ($this->id != 0) {
			$rs = sql_query("SELECT * FROM " . EXCHANGE_TABLE_NAME . " WHERE id=" . $this->id);
			$this->currency_from = $rs->fields['currency_from'];
			$this->currency_to   = $rs->fields['currency_to'];
			$this->rate          = $rs->fields['rate'];
		}
	}
	
	function reload()
	{
		$this->Exchange($this->id);
	}
	
	function delete()
	{
		sql_query("DELETE FROM " . EXCHANGE_TABLE_NAME . " WHERE id=" . $this->id);
	}

	function save()
	{
		global $conn;
	    $sql = "INSERT INTO " . EXCHANGE_TABLE_NAME . " (currency_from, currency_to, rate, created_at, updated_at) VALUES ( '" . $this->currency_from . "', '" . $this->currency_to . "', " . $this->rate . ", NOW(), NOW() )";
		//echo $sql; exit();
		if ($conn->Execute($sql) === false) {
		     if ($conn->ErrorNo() == 1062)
                 return ERROR_1062;
             else {
                 log_error("Erreur MySQL dans la SAUVEGARDE de " . EXCHANGE_TABLE_NAME . " > " . $sql);
                 return ERROR_MYSQL;
             }
        }
        $this->id = $conn->Insert_ID();
        return true;
	}

	function getRate($currency_from, $currency_to, $string=false, $string_updated=false)
	{
		$sql = "SELECT rate, updated_at FROM " . EXCHANGE_TABLE_NAME . " WHERE currency_from='" . $currency_from . "' AND currency_to='" . $currency_to . "' ORDER BY updated_at DESC LIMIT 0,1";
		$rs = sql_query($sql);
		if ( $rs->RecordCount( ) == 0 ) {
			$sql = "SELECT rate, updated_at FROM " . EXCHANGE_TABLE_NAME . " WHERE currency_from='" . $currency_to . "' AND currency_to='" . $currency_from . "' ORDER BY updated_at DESC LIMIT 0,1";
			$rs = sql_query($sql);
			if ( $rs->RecordCount( ) == 0 ) return false;
			$rate = 10 / $rs->fields['rate'];
		} else {
			$rate = $rs->fields['rate'];
		}
		$updated_at = $rs->fields['updated_at'];
		
		$rate = preg_replace('/^0*/i', '', $rate);
		$rate = preg_replace('/^[.]/i', '0.', $rate);
		//$rate = displayNumber($rate);
		if ($string) $rate .= ' '. $currency_to .' / '. EXCHANGE_UNITY .' '. $currency_from;
		if ($string_updated) $rate .= ' <small>(dernière mise à jour le '. usToFrDate($updated_at, true) .')</small>';
		return trim($rate);
	}
/*
	function getAll()
	{
		$data = array();
		$recordSet = sql_query("SELECT * FROM " . EXCHANGE_TABLE_NAME . " ORDER BY currency_to ASC GROUP BY currency_from");
		while (!$recordSet->EOF) {
			$id = $recordSet->fields['id'];
			$data[$id] = new Exchange($id);
			$recordSet->MoveNext();
		}
		return $data;
	}

    // renvoi le code html pour un element formulaire de type select + son label
	function getSelect($selectedId=false)
	{
	    $label = '<label for="localisation_id">Localisation</label>';
        $select = '<select id="localisation_id" name="localisation_id">';
        foreach ($this->getAll() as $id => $tab) {
            $select.= '<option value="' . $id . '"';
            if($selectedId!==false && $selectedId==$id) $select.= ' selected="selected"';
            $select.= '>' . $tab['libelle'] . '</option>';
        }
        $select.= '</select>';
        $html = $label . $select;
        return $html;
	}
*/
}
